package com.mall.shopping.services;

import com.mall.shopping.CategoriesService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.shopping.dto.ProductCateDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

//列举所有商品种类
@Component
@Service(interfaceClass = CategoriesService.class)
public class CategoriesImp implements CategoriesService {

    @Autowired
    ItemCatMapper itemCatMapper;

    @Override
    public AllProductCateResponse getCategories() {
        Example example = new Example(ItemCat.class);
        example.setOrderByClause("sort_order");
        List<ItemCat> items = itemCatMapper.selectByExample(example);
        List<ProductCateDto> productCateDtoList = new ArrayList<>();
        for (ItemCat itemCat : items) {
            ProductCateDto productCateDto = new ProductCateDto();
            productCateDto.setId(itemCat.getId());
            productCateDto.setName(itemCat.getName());
            productCateDto.setParentId(itemCat.getParentId());
            productCateDto.setIsParent(itemCat.getIsParent());
            productCateDto.setIconUrl(itemCat.getIcon());
            productCateDtoList.add(productCateDto);
        }

        AllProductCateResponse response = new AllProductCateResponse();
        response.setProductCateDtoList(productCateDtoList);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return response;

    }
}
